package jp.co.coretechno;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Block {
	public static final int BLOCK_WIDTH = 5;
	public static final int BLOCK_HEIGHT = 5;
	public static final int BLOCK_PIXEL = 16;
	
	private Cell[][] pattern  = new Cell[BLOCK_WIDTH][BLOCK_HEIGHT];
	private Cell[][] patternWork = new Cell[BLOCK_WIDTH][BLOCK_HEIGHT];
	private Random random;
		
	public Block() {
		this.random = new Random();
		setRandomBlock();
	}
	
	public void setRandomBlock() {
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				pattern[y][x] = new Cell();
			}
		}
		
		Color col;
		switch (random.nextInt(7)) {
		case 0:
			col = Color.BLUE;
			pattern[0][2].setCell(col);
			pattern[1][2].setCell(col);
			pattern[2][2].setCell(col);
			pattern[3][2].setCell(col);
			pattern[4][2].setCell(col);
			break;
			
		case 1:
			col = Color.GREEN;
			pattern[0][2].setCell(col);
			pattern[1][2].setCell(col);
			pattern[2][2].setCell(col);
			pattern[2][3].setCell(col);
			break;
			
		case 2:
			col = Color.YELLOW;
			pattern[0][2].setCell(col);
			pattern[1][2].setCell(col);
			pattern[2][2].setCell(col);
			pattern[2][1].setCell(col);
			break;
			
		case 3:
			col = Color.CYAN;
			pattern[1][2].setCell(col);
			pattern[1][3].setCell(col);
			pattern[2][1].setCell(col);
			pattern[2][2].setCell(col);
			break;
			
		case 4:
			col = Color.PINK;
			pattern[1][1].setCell(col);
			pattern[1][2].setCell(col);
			pattern[2][2].setCell(col);
			pattern[2][3].setCell(col);
			break;
			
		case 5:
			col = Color.ORANGE;
			pattern[2][1].setCell(col);
			pattern[3][1].setCell(col);
			pattern[2][2].setCell(col);
			pattern[3][2].setCell(col);
			break;
			
		case 6:
			col = Color.RED;
			pattern[1][2].setCell(col);
			pattern[2][1].setCell(col);
			pattern[2][2].setCell(col);
			pattern[2][3].setCell(col);
			break;
		}	
	}
	
	public boolean isHit(int x, int y) {
		return !pattern[y][x].isEmpty();
	}
	
	public Color getColor(int x, int y) {
		return pattern[y][x].getColor();
	}
	
	public void rotateLeft() {
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				patternWork[BLOCK_HEIGHT - x - 1][y] = pattern[y][x];
			}
		}
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				pattern[y][x] = patternWork[y][x];
			}
		}
	}
	
	public boolean canRotateLeft(Field field, int blockX, int blockY) {
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				patternWork[BLOCK_HEIGHT - x - 1][y] = pattern[y][x];
			}
		}
		
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {	
				if (field.isHit(x + blockX, y + blockY)) {
					return false;
				}
			}
		}
		
		return true;
	}
	
	public void rotateRight() {
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				patternWork[BLOCK_HEIGHT - x - 1][y] = pattern[y][x];
			}
		}
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				pattern[y][x] = patternWork[y][x];
			}
		}
	}
	
	public boolean canrotateRight(Field field, int blockX, int blockY) {
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				patternWork[BLOCK_HEIGHT - x - 1][y] = pattern[y][x];
			}
		}
		
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {	
				if (field.isHit(x + blockX, y + blockY)) {
					return false;
				}
			}
		}
		
		return true;
	}
	
	public void draw(Graphics g, int blockX, int blockY) {
		for (int y = 0; y < BLOCK_HEIGHT; y++) {
			for (int x = 0; x < BLOCK_WIDTH; x++) {
				pattern[y][x].draw(g, blockX + x, blockY + y);
			}
		}
	}
}
